using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
namespace sCicero
{
    public struct Test
    {
        public DataRow oTest;
        public DataTable oFilesTable;
    }

    public class Tests : Base
    {
        private const string TESTS = "tests";
        private Test m_oTest;

        public Tests(ref Data oData)
        {
            m_Table = TESTS;
            m_oData = oData;
        }

        public Test GetObject(int nTestID)
        {
            try
            {
                m_oTest.oTest = Get(nTestID);
                string sWhere = "";
                foreach (DataRow oDr in m_oData.GetGenericDataTable("fileslink_tests").Select("test_id = " + nTestID.ToString()))
                {
                    sWhere += oDr["file_id"].ToString() + ",";
                }
                m_oTest.oFilesTable = null;
                if (sWhere.Length > 0)
                {
                    sWhere = sWhere.Substring(0, sWhere.Length - 1);
                    //m_oTest.oFiles = m_oData.GetGenericDataTable("files").Select("id in (" + sWhere + ")");
                    m_oTest.oFilesTable = m_oData.GetDataTablefromRows(m_oData.GetGenericDataTable("files").Select("id in (" + sWhere + ")"));
                }
            }
            catch (Exception ex)
            {
                m_LastError = ex.Message;
                m_LastReturnCode = Errorcode.DATABASE_ERROR;
            }
            return m_oTest;
        }
        public DataRow[] Search(string sSearchString)
        {
            return m_oData.GetGenericDataTable(TESTS).Select("tag like '%" + sSearchString + "%' or name like '%" + sSearchString + "%'");
        }

        public Errorcode Edit(int nTestId, string sFileName, string sName, int nSubjectId, string sTag)
        {
            cFile oFile = new cFile();
            DataRow o_Dr = m_oData.GetGenericDataTable(TESTS).Rows.Find(nTestId);
            o_Dr["name"] = sName;
            //ta hand om filen och spara ner den
            if (sFileName != "" && sFileName != o_Dr["filename"].ToString())
            {
                try
                {
                    if (oFile.Info(sFileName).Exists)
                    {
                        oFile.Save(sFileName, o_Dr["id"].ToString());
                    }
                    o_Dr["filename"] = sFileName;
                }
                catch
                {
                    o_Dr["filename"] = "";
                }
            }
            o_Dr["subject_id"] = nSubjectId;
            o_Dr["tag"] = sTag;
            m_oData.AcceptChanges(TESTS);
            return Errorcode.NOERROR;
        }
        
        public Errorcode New(string sFileName, string sName, int nSubjectId, string sTag)
        {
            cFile oFile = new cFile();

            DataRow o_Dr = m_oData.GetGenericDataTable(TESTS).NewRow();
            o_Dr["name"] = sName;
            //ta hand om filen och spara ner den
            try
            {
                if (oFile.Info(sFileName).Exists)
                {
                    oFile.Save(sFileName, o_Dr["id"].ToString());
                }
                o_Dr["filename"] = sFileName;
            }
            catch
            {
                o_Dr["filename"] = "";
            }
            o_Dr["subject_id"] = nSubjectId;
            o_Dr["tag"] = sTag;
            m_oData.GetGenericDataTable(TESTS).Rows.Add(o_Dr);
            m_oData.AcceptChanges(TESTS);
            return Errorcode.NOERROR;
        }
       



    }
}
